import Mock from 'mockjs';

Mock.setup({
  timeout: '300-600'
});

// 登录接口（POST /api/login）
Mock.mock('/api/login', 'post', (options) => {
  const body = JSON.parse(options.body);
  const { type, account, email, password } = body;

  const isValid =
    (type === 'password' && account && password) ||
    (type === 'email' && email && password);

  if (isValid) {
    return {
      success: true,
      user: {
        id: Mock.Random.guid(),
        username: account || email,
        token: Mock.Random.string('abcdef0123456789', 32)
      }
    };
  } else {
    return {
      success: false,
      message: '登录信息不完整'
    };
  }
});

// 注册接口（POST /api/register）
Mock.mock('/api/register', 'post', (options) => {
  const body = JSON.parse(options.body);
  const { username, email, password, invitationCode } = body;

  if (username && email && password && invitationCode) {
    return {
      success: true,
      user: {
        id: Mock.Random.guid(),
        username,
        token: Mock.Random.string('abcdef0123456789', 32)
      }
    };
  } else {
    return {
      success: false,
      message: '注册信息不完整'
    };
  }
});

// 获取用户信息接口（GET /user/info）
Mock.mock('/user/info', 'get', {
  success: true,
  user: {
    id: Mock.Random.guid(),
    username: Mock.Random.cname(),
    email: Mock.Random.email(),
    avatar: Mock.Random.image('100x100', '#50B347', '#FFF', 'User')
  }
});
